package com.seleniti.enpareja.android.ui.widget;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

public class WeHaveDrawable extends Drawable {
	private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	private boolean weHave;
	private Integer width;
	private Integer height;
	private int padding;
	private Integer minHeight;

	public WeHaveDrawable(boolean weHave, Integer width, Integer height, Integer padding) {
		this(weHave);
		this.width = width;
		this.height = height;
		this.padding = padding != null ? padding : 0;
	}

	public WeHaveDrawable(boolean weHave) {
		this.weHave = weHave;
	}

	@Override
	public void draw(Canvas canvas) {
		Rect bounds = canvas.getClipBounds();
		int w = this.width != null && this.width > 0 ? this.width : bounds.left + bounds.right;
		int h = this.height != null && this.height > 0 ? this.height : bounds.top + bounds.bottom;

		w -= this.padding * 2;
		h -= this.padding * 2;

		int startX = this.padding;
		int startY = this.padding;

		if (minHeight != null) {
			h = Math.max(h, minHeight);
		}

		canvas.drawColor(Color.WHITE);
		canvas.save();
		paint.setColor(weHave ? Color.parseColor("#008000") : Color.RED);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(1.0f);
		paint.setAntiAlias(true);
		paint.setDither(true);
		if (!weHave) {
			canvas.drawLine(startX, startY, w - 20, startY, paint);
			canvas.drawLine(w - 20, startY, w, h / 2, paint);
			canvas.drawLine(w, h / 2, w - 20, h, paint);
			canvas.drawLine(w - 20, h, startX, h, paint);
			canvas.drawLine(startX, startY, startX, h, paint);
		} else {
			canvas.drawLine(20, 0, w, 0, paint);
			canvas.drawLine(w, 0, w, h, paint);
			canvas.drawLine(w, h, 20, h, paint);
			canvas.drawLine(20, h, 0, h / 2, paint);
			canvas.drawLine(0, h / 2, 20, 0, paint);
		}
		canvas.restore();
	}

	@Override
	public void setAlpha(int alpha) {
	}

	@Override
	public void setColorFilter(ColorFilter cf) {
	}

	@Override
	public int getOpacity() {
		return PixelFormat.OPAQUE;
	}

	public Integer getMinHeight() {
		return minHeight;
	}

	public void setMinHeight(Integer minHeight) {
		this.minHeight = minHeight;
	}

}